<?php

namespace app\admin\controller;

use app\model\Geocoder;
use app\model\StoreCate;
use think\exception\ValidateException;
use app\model\Store;
use app\model\StoreLevel;
use app\model\Category;
use app\model\RegisterField;
use app\model\Operatingcity;
use app\model\Tuanzhang;
use app\model\Users;

class StoreController extends Base
{

	function index()
	{
		$weid = weid();
		$page = input('post.page', 1, 'intval');
        $cate = input('post.cate', '', 'serach_in');
		$ptype = 'store';

		$Fielddata = RegisterField::where(['weid' => $weid, 'ptype' => $ptype])->select()->toArray();

		if (empty($Fielddata)) {
			RegisterField::datainitial($ptype);
		}

		$query = $this->setSearch();

		$res = $query->order('sort asc,id desc')->paginate(getpage())->toArray();
        $store_cate = StoreCate::where([
            'weid' => weid(),
            'status' => 1
        ])->field('id,title')->order('sort asc')->select();
        foreach ($store_cate as $k1=>$v1 ){
            $store_cate[$k1]['val'] = $v1['id'];
            $store_cate[$k1]['key'] = $v1['title'];
        }


		if (!empty($res['data'])) {
			foreach ($res['data'] as &$vo) {
                $cate = StoreCate::where(['id' => $vo['cate']])->find();
                $vo['cate'] = $cate['title'];
				$vo = Store::conversion($vo);
			}
		}
		$data['data'] = $res;
        $data['store_cate'] = $store_cate;
		if ($page == 1) {
			$data['field_data']['RegisterField'] = RegisterField::getlistViewField($ptype);
		}

		return $this->json($data);
	}
	function setSearch()
	{
		$keyword = trim(input('post.keyword', '', 'serach_in'));
		$status = input('post.status', '', 'serach_in');
		$path = input('post.path', '', 'serach_in');
        $cate = input('post.cate', '', 'serach_in');
		$weid = weid();

		if ($path == "/store/audit") {
			$status = "0";
		}

		$query = Store::where(['weid' => $weid, 'tzid' => 0]);

		if (!empty($this->ocid)) {
			$Operatingcitydata = Operatingcity::find($this->ocid);
			if ($Operatingcitydata) {
				$Operatingcitydata = $Operatingcitydata->toArray();
				if (empty($Operatingcitydata['areatype'])) {
					$Operatingcitydata['areatype'] = 3;
				}

				if ($Operatingcitydata['areatype'] == 3) {
					$query->where('district_name', $Operatingcitydata['district_name']);
				} elseif ($Operatingcitydata['areatype'] == 2) {
					$query->where('city_name', $Operatingcitydata['city_name']);
				} elseif ($Operatingcitydata['areatype'] == 1) {
					$query->where('province_name', $Operatingcitydata['province_name']);
				}
			}
		}
		if (!empty($this->tzid)) {

			$Tuanzhangdata = Tuanzhang::find($this->tzid);

			if ($Tuanzhangdata) {
				$Tuanzhangdata = $Tuanzhangdata->toArray();
				$query->where('district_name', $Tuanzhangdata['district_name']);
			} else {
				$query->where('district_name', '无');
			}
		}

		if (!empty($keyword)) {

			$query->where('title|tel', 'like', '%' . $keyword . '%');
		}

		if (!empty($status) || $status === "0") {
			$query->where(['status' => $status]);
		}

        if(!empty($cate)){
            $query->where('cate', $cate);
        }

		return $query;
	}
	function listUpdate()
	{
		$data = only('id,status');
		if (!$data['id']) throw new ValidateException('参数错误');
		Store::update($data);

		return $this->json(['msg' => '操作成功']);
	}

	function getInfo()
	{
		$id =  $this->sid;

		if ($id) {
			$data = Store::find($id)?->toArray();
		}

		return $this->json(['data' => $data]);
	}
	public function update()
	{
		$id = request()->post('id');
		$data = input('post.');
		unset($data['create_time']);
		if (!empty($id)) {

			try {
				Store::update($data);
			} catch (\Exception $e) {
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '修改成功']);
		}
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		if (!is_array($idx)) {
			$idx = explode(',', $idx);
		}
		foreach ($idx as $vo) {
			$deldata = Store::where('id', $vo)->find();
			if (!empty($deldata)) {
				if (!empty($deldata->id)) {
					Users::where('sid', $deldata->id)->delete();
				}
				Store::where('id', $deldata->id)->delete();
			}
		}
		return $this->json(['msg' => '操作成功']);
	}
	//导出
	function dumpdata()
	{
		$page = $this->request->post('page', 1, 'intval');
		$ptype = 'store';
		$query = $this->setSearch();
		$data = RegisterField::dumpdata($query, $ptype, $page);
		return $this->json($data);
	}

    public function get_store_cate()
    {
        if($this->request->isPost()){
            $page = $this->request->post('page', 1, 'intval');
            $limit = $this->request->post('limit', 10, 'intval');
            $map = [
                'weid' => weid(),
                'status' => 1
            ];
            $data = StoreCate::where($map)->limit(($page - 1) * $limit, $limit)->order('sort asc, id desc')->select();
            $count = StoreCate::where($map)->count();
            return $this->json(['code' => 0, 'msg' => 'success', 'data' => ['count' => $count, 'item' => $data]]);
        }
    }

    public function add_store_cate()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $data['weid'] = weid();
            $state = StoreCate::create($data);
            if($state != false){
                return $this->json(['code' => 0, 'msg' => '添加成功']);
            }else{
                return $this->json(['code' => 1, 'msg' => '添加失败']);
            }
        }
    }

    public function edit_store_cate()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $state = StoreCate::update($data);
            if($state != false){
                return $this->json(['code' => 0, 'msg' => '修改成功']);
            }else{
                return $this->json(['code' => 1, 'msg' => '修改失败']);
            }
        }
    }

    public function del_store_cate()
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $state = StoreCate::where('id',$id)->update(['status' => 0]);
            if($state != false){
                return $this->json(['code' => 0, 'msg' => '删除成功']);
            }else{
                return $this->json(['code' => 1, 'msg' => '删除失败']);
            }
        }
    }
}
